package org.openfidias.jdbase.view;

import java.io.IOException;
import java.util.Scanner;
import org.apache.commons.lang.StringUtils;
import org.openfidias.jdbase.lang.Choose;

/**
 *
 * @author atila
 */
public class Console {

   public static final Scanner read = new Scanner(System.in).useDelimiter("\n");
   public static final String bash = "jdbase> ";

   private static void menu() {
      clearConsole();
      StringBuilder builder = new StringBuilder();
      builder.append(StringUtils.center(" \u03C6 jDBase - Java Data Base \u03C6 ", 60, "·"));
      builder.append("\n");
      builder.append(StringUtils.center(" Menu Principal ", 60, " · "));
      builder.append("\n\n");
      builder.append("[.t aluno] Tabela Aluno\n");
      builder.append("[.t disciplina] Tabela Disciplina\n");
      builder.append("[.t aluno_disciplina] Tabela Aluno_Disciplina\n");
      builder.append("[.quit][.back][.clear]");
      System.out.println(builder.toString());
   }

   public static void begin() throws IOException, Exception {
      String option = "";
      while (true) {
         menu();
         System.out.print(bash);
         option = read.nextLine();
         if (Choose.is(".t aluno", option)) {
            AlunoView.getInstance().begin();
         } else if (Choose.is(".t disciplina", option)) {
            // call disiciplina bo begin
            DisciplinaView.getInstance().begin();
         } else if (Choose.is(".t aluno_disciplina", option)) {
            // call aluno_disiciplina bo begin
            continue;
         } else if (Choose.is(".quit", option)) {
            // terminar programa.
            System.exit(0);
         } else if (Choose.is(".clear", option)) {
            clearConsole();
         } else {
            System.out.println("Opção Inválida.");
         }
      }
   }

   public static void clearConsole() {
      /* testar SO para Linux ou Windows */
      System.out.printf("\033c");
   }
}
